package com.my.sec;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.my.dao.DAO;
import com.my.dao.impl.DAOImpl;

public class CustomInvocationSecurityMetadataSourceService {

	DAO dao = new DAOImpl();
	private final Map<String, List<String>> requestMap = new LinkedHashMap<String, List<String>>();

	public CustomInvocationSecurityMetadataSourceService() {
		super();
		loadResourceDefine();
	}
	
	// 加载所有资源与权限的关系
	public void loadResourceDefine() {
		String hql = "select ra.application.url, ra.role.name from RoleAuth ra join ra.application join ra.role";
		List list = dao.queryByHQL(hql);;
		for(Object o: list) {
			Object[] obj = (Object[]) o;
			String url = (String) obj[0];
			String role = (String) obj[1];
			System.out.println(obj[0] + "-" + obj[1]);
			if(requestMap.containsKey(url)) {
				requestMap.get(url).add(role);
			} else {
				List l = new ArrayList();
				l.add(role);
				requestMap.put(url, l);
			}
		}
		
		System.out.println(requestMap);
	}

	// 返回所请求资源所需要的权限
	public List<String> getAttributes(Object object) {
		return requestMap.get(object);
	}
	
	public static void main(String[] args) {
		new CustomInvocationSecurityMetadataSourceService();
	}
}